﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CashResource;
using CashCommon;
using Cash.Model;

namespace Cash.DialogFroms
{
    public partial class FormAccountOperation : Form
    {
        public FormAccountOperation()
        {
            InitializeComponent();
            this.Text = CommonText.AccountOperationFormTitle;
        }

        #region События
        
        private void btnRegisterPrint_Click(object sender, EventArgs e)
        {
            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.DoWork += new DoWorkEventHandler(bgwShowReport);

            RunWorkerCompletedEventHandler rwc = new RunWorkerCompletedEventHandler(bgwCompletedShowReport);
            _backgroundWorker.RunWorkerCompleted += rwc;
            _backgroundWorker.RunWorkerAsync();

            FormReports.frmProgressCancel += rwc;
            FormReports.Born(dtReportsDate.Value);
        }
        private void btnPaiesToOmnius_Click(object sender, EventArgs e)
        {
            _frmProgress = new FrmProgress(CommonText.RegisterFormTitlePost);
            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.DoWork += new DoWorkEventHandler(bgwPostDocuments);
            _backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwCompletedPostDocuments);
            _backgroundWorker.RunWorkerAsync();
            _frmProgress.ShowDialog();
            if (OnCheckPosted != null)
                OnCheckPosted();
        }
        private void FormAccountOperation_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            Tools.GetHelp("operation_account", HelpNavigator.Topic);
            e.Cancel = true;
        }
        private void FormAccountOperation_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
                FormAccountOperation_HelpButtonClicked(null, new CancelEventArgs());
        }
        
        #endregion

        #region Helper

        // Печать реестра
        private void bgwShowReport(object sender, DoWorkEventArgs e)
        {
            _frmProgress = new FrmProgress(CommonText.ProgressFormTitleReport);
            _frmProgress.ShowDialog();
        }
        private void bgwCompletedShowReport(object sender, RunWorkerCompletedEventArgs e)
        {
            if(_frmProgress != null)
                _frmProgress.DialogResult = DialogResult.OK;
        }
        
        // Проведение документов
        private void bgwPostDocuments(object sender, DoWorkEventArgs e)
        {
            int fDivision = Convert.ToInt32(CashEmployer.Inst.F_Division.Split(',').ElementAt(1));
            PostResult postResult = CashRegister.PostDocuments(dtpPostDate.Value, fDivision);

            if (this.tbPostResult.InvokeRequired)
                BeginInvoke(new MethodInvoker(delegate
                {
                    tbPostResult.Text = String.Format(CommonText.ResultOfPost, 
                        postResult.DatePost.ToShortDateString(),
                        postResult.Count,
                        postResult.Sum
                        );
                }));
            else
                tbPostResult.Text = String.Format(CommonText.ResultOfPost,
                    postResult.DatePost.ToShortDateString(),
                    postResult.Count,
                    postResult.Sum
                    );
        }
        private void bgwCompletedPostDocuments(object sender, RunWorkerCompletedEventArgs e)
        {
            if (_frmProgress != null)
                _frmProgress.DialogResult = DialogResult.OK;
        }

        #endregion

        public event DlegateWithoutParams OnCheckPosted;

        private BackgroundWorker _backgroundWorker;
        private FrmProgress _frmProgress;

    }
}
